package com.graduationproject.examonline.notice.service;

import com.graduationproject.examonline.notice.entity.Notice;
import com.graduationproject.examonline.notice.mapper.NoticeMapper;
import com.graduationproject.examonline.teacher.entity.Teacher;
import com.graduationproject.examonline.teacher.mapper.TeacherMapper;
import com.graduationproject.examonline.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    public Result getNoticeByUserId(String userId) {
        try {
            List<Notice> noticeList = noticeMapper.getNoticeByUserId(userId);
            return new Result(0, 200, "查询成功", noticeList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "查询失败");
        }
    }

    public Result getNoticeByCreateUser(String userId) {
        try {
//            List<Notice> noticeList = noticeMapper.getNoticeByCreateUser(userId);
//            List<Map<String,Object>> nextNoticeList = new LinkedList<Map<String,Object>>();
//            if (noticeList.size() > 0) {
//                for (int index = 0; index < noticeList.size(); index += 1) {
//                    String create_user_id = noticeList.get(0).getCreate_user_id();
//                    List<Teacher> record = teacherMapper.getUserInfo(create_user_id);
//                    Map<String, Object> temp = new HashMap<String,Object>();
//                    temp.put("notice_id",noticeList.get(0).getNotice_id());
//                    temp.put("context",noticeList.get(0).getContext());
//                    temp.put("create_date",noticeList.get(0).getCreate_date());
//                    temp.put("notice_id",noticeList.get(0).get());
//                    if(record.size()>0){
//                        nextNoticeList.add("notice_id",noticeList.get(0).getNotice_id());
//                    }
//                }
//            }
            return new Result(0, 200, "ok");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "error");
        }
    }

    public Result checkNotice(String noticeId) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("noticeId", noticeId);
            params.put("unread", 1);
            int recordAmount = noticeMapper.updateNotice(params);
            if (recordAmount > 0) {
                return new Result(0, 200, "操作成功");
            } else {
                return new Result(1, 200, "操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "操作失败");
        }
    }

    public Result deletedNotice(String noticeId) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("noticeId", noticeId);
            params.put("deleted", 1);
            int recordAmount = noticeMapper.updateNotice(params);
            if (recordAmount > 0) {
                return new Result(0, 200, "操作成功");
            } else {
                return new Result(1, 200, "操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "操作失败");
        }
    }

    public Result createNotice(Map<String, Object> params) {
        try {
            params.put("notice_id", System.currentTimeMillis());
            params.put("unread", 0);
            params.put("create_date", new SimpleDateFormat().format("YYYY-MM-DD HH:mm:ss"));
            params.put("deleted", 0);
            int recordAmount = noticeMapper.insertNotice(params);
            if (recordAmount > 0) {
                return new Result(0, 200, "操作成功");
            } else {
                return new Result(1, 200, "操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(1, 500, "发送失败");
        }
    }
}
